package com.sip.stream.utils.zfy;

import android.util.Log;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.sip.stream.utils.zfy.muxer.BarSteamTool;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class VideoEncryptUtils {
    private static final String TAG = "VideoEncryptUtils";

    public static void encrypt(String filePath) {
        byte[] bArr;
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(new File(filePath), "rw");
            randomAccessFile.read(new byte[24]);
            byte[] bArr2 = new byte[24];
            for (int i = 0; i < 24; i++) {
                bArr2[i] = 1;
            }
            randomAccessFile.seek(0L);
            randomAccessFile.write(bArr2);
            randomAccessFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteDecryptCache() {
        try {
            FileUtils.deleteAllInDir(ZFYFilePath.getInstance().getAbsoluteFileDir(ZFYFilePath.CACHE) + "/decrypt" + File.separator);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String decrypt(String filaPath) {
        RandomAccessFile randomAccessFile = null;
        byte[] bArr = null;
        if (filaPath.endsWith(".mp4"))
        {
            try {
                File file = new File(filaPath);
                File file2 = new File((ZFYFilePath.getInstance().getAbsoluteFileDir(ZFYFilePath.CACHE) + "/decrypt" + File.separator) + file.getName());
                FileUtils.copy(file, file2);
                try {
                    randomAccessFile = new RandomAccessFile(file2, "rw");
                    bArr = new byte[24];
                    randomAccessFile.read(bArr);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (BarSteamTool.byteToHex(bArr).equalsIgnoreCase("00000018667479706d7034320000000069736f6d6d703432")) {
                    return filaPath;
                }
                byte[] hexString2Bytes = ConvertUtils.hexString2Bytes("00000018667479706d7034320000000069736f6d6d703432");
                randomAccessFile.seek(0L);
                randomAccessFile.write(hexString2Bytes);
                randomAccessFile.close();
                return file2.getAbsolutePath();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return filaPath;
    }
}
